package com.yuanmei.charts;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spanned;

import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yuanmei.views.R;
import com.yuanmei.common.Utils;

import java.util.ArrayList;
import java.util.List;

public class MACDChartDataSet implements TaChartData {
    private List<BarEntry> mMACDList;
    private List<Entry> mDEAList;
    private List<Entry> mDIFList;
    private List<String> mXAxisDates;
    private CombinedData mMACDCombineData;
    private Context mContext;
    private KLineChartDataSet mRawDataSet;

    public MACDChartDataSet(Context context, KLineChartDataSet rawDataSet) {
        mContext = context;
        mRawDataSet = rawDataSet;
        mDEAList = new ArrayList<>();
        mDIFList = new ArrayList<>();

        mMACDList = new ArrayList<>();
        mXAxisDates = new ArrayList<>();

        List<Integer> colors = new ArrayList<Integer>();
        int green = Color.rgb(110, 190, 102);
        int red = Color.rgb(211, 74, 88);

        int fastEmaPeriod = 12;
        int slowEmaPeriod = 26;
        int deaPeriod = 9;
        float slowEma = 0.0f, fastEma = 0.0f, dif = 0.0f, dea = 0.0f, macd = 0.0f;
        for (int i = 0; i < mRawDataSet.getXAxisSize(); i++) {
            mXAxisDates.add(mRawDataSet.getXAxisLabel(i));
            float close = mRawDataSet.getClose(i);
            if (i == 0) {
                fastEma = close;
                slowEma = close;
            } else {
                fastEma = TaLib.emaSummary(fastEma, close, fastEmaPeriod);
                slowEma = TaLib.emaSummary(slowEma, close, slowEmaPeriod);
            }
            dif = fastEma - slowEma;
            dea = TaLib.emaSummary(dea, dif, deaPeriod);
            macd = (dif - dea) * 2;

            if (macd >= 0)
                colors.add(red);
            else
                colors.add(green);

            mDIFList.add(new Entry(i, dif));
            mDEAList.add(new Entry(i, dea));
            mMACDList.add(new BarEntry(i, macd));
        }

        BarDataSet barSet = new BarDataSet(mMACDList, "MACD");
        barSet.setHighlightEnabled(true);
        barSet.setHighLightAlpha(255);
        barSet.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        barSet.setDrawValues(false);

        barSet.setColors(colors);
        barSet.setValueTextColors(colors);
        BarData barData = new BarData(barSet);

        ArrayList<ILineDataSet> lineDataSet = new ArrayList<>();
        LineDataSet difSet =  new LineDataSet(mDIFList, "DIF");
        TaLib.setCommonMaStyle(difSet);
        difSet.setHighlightEnabled(true);
        difSet.setDrawHorizontalHighlightIndicator(true);
        difSet.setDrawVerticalHighlightIndicator(true);
        difSet.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        difSet.setColor(mContext.getResources().getColor(R.color.ma5));
        lineDataSet.add(difSet);

        LineDataSet deaSet =  new LineDataSet(mDEAList, "DEA");
        TaLib.setCommonMaStyle(deaSet);
        deaSet.setColor(mContext.getResources().getColor(R.color.ma10));
        lineDataSet.add(deaSet);

        LineData smaLinesData = new LineData(lineDataSet);

        mMACDCombineData = new CombinedData();
        mMACDCombineData.setData(barData);
        mMACDCombineData.setData(smaLinesData);
    }

    @Override
    public int getXAxisSize() {
        return mXAxisDates.size();
    }

    @Override
    public String getXAxisLabel(int index) {
        String date = "None";
        if (index >= 0 && index < mXAxisDates.size()) {
            date = String.valueOf(mXAxisDates.get(index));
        }
        return date;
    }

    @Override
    public Spanned getDescription(int index) {
        String macd = "MACD:";
        if (index >= 0 && index < mMACDList.size()) {
            macd += Utils.priceToDecimalString(mMACDList.get(index).getY());
        }

        String dif = ", DIF:";
        if (index >= 0 && index < mDIFList.size()) {
            dif += Utils.priceToDecimalString(mDIFList.get(index).getY());
        }

        String dea = ", DEA:";
        if (index >= 0 && index < mDEAList.size()) {
            dea += Utils.priceToDecimalString(mDEAList.get(index).getY());
        }
//        return  macd + dif + dea;
        String html_str = String.format(
                        "%s<font color='#bbbbbb'>%s</font><font color='#E7B448'>%s</font>",
                        macd, dif, dea);
        return  Html.fromHtml(html_str);
    }

    @Override
    public List<String> getXAxisLabelList() {
        return mXAxisDates;
    }

    @Override
    public CombinedData getTaChartData() {
        return mMACDCombineData;
    }
}
